﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	InfoPoint
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

////////////////////////////////
//
//      struttura tabella Point
//
///////////////////////////////////
//
//      codice_point                        ( codice_point - primary key ) 
//      denominazione                       ( codice_denom ) 
//      codice simbolo                      ( simbolo )
//      parametri                           ( parametri )
//      file .bmp                           ( colore )
//      dimensione                          ( dimensione )
//      xxxxxxxxxx                          ( colore1 ) 
//
//////////////////////////////////////   

namespace Map_Fvg
{
    public partial class InfoPoint : Form
    {
        public static int init;
        private string fileName;

        public InfoPoint()
        {
            InitializeComponent();

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from classi";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            comboBox1.Items.Add(reader.GetString(0).ToString() + " - " + reader.GetString(1).ToString());
                        }
                    }
                    conn.Close();
                }
                comboBox1.SelectedIndex = 0;
            }

            if (init == 1)
            {
                textBox1.ReadOnly = false;
                button1.Enabled = false;
                button5.Enabled = true;
                label1.Text = "inserimento di nuovo simbolo grafico";
                comboBox1.SelectedIndex = 0;
                return;
            }
            textBox1.ReadOnly = true;
            button5.Enabled = false;

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "select * from point WHERE codice_point = '";
                    //                   ss = string.Concat(ss, ViewQ.selected_point, "'");
                    ss = string.Concat(ss, Simbol.selectedLinea, "'");
                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            textBox1.Text = reader.GetString(0).ToString();     //  codice sottoclasse
                            textBox2.Text = reader.GetString(1).ToString();     //  denom. proprietà
                            textBox3.Text = reader.GetString(2).ToString();     //  codice simbolo
                            textBox4.Text = reader.GetString(3).ToString();     //  parametri
                            textBox6.Text = reader.GetString(4).ToString();     //  nome file ( colore )
                            textBox7.Text = reader.GetString(5).ToString();     //  dimensione   
                 //           textBox5.Text = reader.GetString(6).ToString();    //  colore1
                        }
                    }
                }
                conn.Close();
            }
            string ss1 = textBox1.Text.Substring(0, 1);
            int ipos = int.Parse(ss1);
            comboBox1.SelectedIndex = ipos - 1;
            comboBox1.Enabled = false;

            fileName = textBox6.Text.Trim();
            if (!string.IsNullOrEmpty(fileName))  panel1.Invalidate();
            
        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  conferma
            string s2 = textBox2.Text;
            if (s2.Length == 0)
            {
                MessageBox.Show("manca denominazione proprietà");
                return;
            }
            string s3 = textBox3.Text;
            if (s3.Length == 0)
            {
                MessageBox.Show("indicare codice simbolo");
                return;
            }

            string s4 = textBox4.Text;
            if (s4.Length == 0)
            {
                MessageBox.Show("indicare parametri simbolo");
                return;
            }

            string s5 = textBox6.Text;

            string s7 = textBox7.Text;;
            string s8 = " ";

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "update point set codice_denom = '" + s2 + "', simbolo = '" + s3 + "', parametri ='" + s4 +
                            "', colore ='" + s5 + "', dimensione ='" + s7 + "', colore1 ='" + s8 + "' WHERE codice_point = '";
                    ss = string.Concat(ss, Simbol.selectedLinea, "'");

                    cmd.CommandText = ss;

                    cmd.ExecuteNonQuery();
                }


                conn.Close();
            }

            this.Close();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            //  annulla

            this.Close();
        }


        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
            }

            Font ifont = new Font(ss, sizef);
            return (ifont);


        }
 
        private void button5_Click(object sender, EventArgs e)
        {

            //  inserimento

            string s1 = textBox1.Text;
            if (s1.Length == 0)
            {
                MessageBox.Show("manca sottoclasse");
                return;
            }

            string s2 = textBox2.Text;
            if (s2.Length == 0)
            {
                MessageBox.Show("manca denominazione simbolo");
                return;
            }
            string s3 = textBox3.Text;
            if (s3.Length == 0)
            {
                MessageBox.Show("indicare codice simbolo");
                return;
            }

            string s4 = textBox4.Text;
            if (s4.Length == 0)
            {
                MessageBox.Show("indicare parametri simbolo");
                return;
            }

            string s5 = textBox6.Text;


            string s7 = " ";

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from point where codice_point = '" + s1 + "'";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        MessageBox.Show("esiste già un altro elemento con lo stesso codice punto " + s1);
                        return;
                    }
                    conn.Close();
                }
            }

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "insert into point values('" + s1 + "','" + s2 + "','" + s3 + "','" + s4 + "','" + s5 + "','" + s7 + "','" + s7 + "')";

                    cmd.CommandText = ss;
                    cmd.ExecuteNonQuery();

                }
                conn.Close();
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }


        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (string.IsNullOrEmpty(fileName)) return;
            string fileX = FormIniz.bitmapFolder;
            fileX = string.Concat(fileX, fileName);

            Graphics dc = this.panel1.CreateGraphics();

            Image newImage = Image.FromFile(fileX);
            Point ulCorner = new Point(0, 0);
            Rectangle destRect = new Rectangle(0, 0, panel1.Size.Width, panel1.Size.Height);

            dc.DrawImage(newImage, destRect);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog Dialog1 = new OpenFileDialog();
            Dialog1.Filter = "bmp files (*.jpg)|*.bmp|All files (*.*)|*.*";
            Dialog1.FilterIndex = 2;

            if (Dialog1.ShowDialog() == DialogResult.OK)
            {
                string fileX = Dialog1.FileName;
                int ipos = fileX.LastIndexOf('\\');
                int length = fileX.Length;
                ++ipos;
                fileName = fileX.Substring(ipos, length - ipos);
                textBox6.Text = fileName;

                panel1.Invalidate();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (init != 1) return;
            string ss = comboBox1.Text;
            int ipos = ss.IndexOf('-', 0);
            string codice = ss.Substring(0, ipos - 1);
            textBox1.Text = string.Concat(codice, "P");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

 




    }
}
